package com.parktech.sun.web.controller;

import com.parktech.sun.domain.model.Constants;
import com.parktech.sun.domain.model.aboutus.AboutUs;
import com.parktech.sun.domain.model.sunhz.Industry;
import com.parktech.sun.domain.model.sunhz.IndustryCategory;
import com.parktech.sun.domain.repository.Page;
import com.parktech.sun.domain.service.aboutus.AboutUsService;
import com.parktech.sun.domain.service.sunhz.IndustryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

/**
 * User: Think
 * Date: 11-1-16
 * Time: 下午12:57
 */
@Controller
@RequestMapping("/industries")
public class IndustryViewController extends BaseViewController {

    private int pageSize = Constants.PAGINATION_PAGE_SIZE;

    private IndustryService industryService;

    @Autowired
    private AboutUsService aboutUsService;

    @Autowired
    public IndustryViewController(IndustryService industryService) {
        this.industryService = industryService;
    }

    @RequestMapping
    public void view(Model model) {
        List<IndustryCategory> categoryList = industryService.listAllCategory();
        model.addAttribute("categoryList", categoryList);
        model.addAttribute(aboutUsService.getByCategory("industryIntroduction"));
        for (IndustryCategory industryCategory : categoryList) {
            industryCategory.setTopIndustryList(industryService.listTop5Industry(industryCategory.getId()));
        }
    }
    @RequestMapping("/categories/{categoryId}/{pageNo}")
    public String viewCategory(@PathVariable("categoryId") Long categoryId, @PathVariable("pageNo") int pageNo, Model model) {
        Page page = new Page(pageSize, pageNo);
        IndustryCategory category = industryService.getByCategoryId(categoryId);
        model.addAttribute(category);
        Page industryPage = industryService.listIndustryByCategory(categoryId, page);
        model.addAttribute(industryPage);
        return "categoryIndustries";
    }
    @RequestMapping("/{industryId}")
    public String viewIndustry(@PathVariable("industryId") Long industryId, Model model) {
        Industry industry = industryService.getById(industryId);
        model.addAttribute(industry);
        return "industryDetail";
    }
}
